package com.css.bjdt_core.report.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.executive.entity.Exp;
import com.css.bjdt_core.executive.report.ReportCache;
import com.css.bjdt_core.report.entity.RptTemplateValidation;
import com.css.bjdt_core.report.entity.RptTemplateValidation.ValidationType;
import com.css.bjdt_core.reportPrepare.entity.RptBatchTemplateRel;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.util.PageQuerys;

@Service
public class ReportTemplateValidationService {
	@Autowired
	private Dao dao;

	private ReportCache cache = ReportCache.getInstance();

	private final static String CACHE_NAME = ReportTemplateValidationService.class.getSimpleName();

	//单个校核式的保存
	public RptTemplateValidation insert(RptTemplateValidation record) {
		if (record == null) {
			return null;
		}
		if (StringUtils.isBlank(record.getValidationId())) {
			record.setValidationId(CommonUtil.getUUID());
		}
		return dao.insert(record);
	}

	//批量保存该报表的校核公式
	public void insertList(List<RptTemplateValidation> list) {
		for (RptTemplateValidation rptTemplateValidation : list) {
			insert(rptTemplateValidation);
		}
	}

	//通过报表ID和校核式的类型删除一类校核式
	public void deleteValidationByTemplateId(String templateID, String validationType) {
		dao.clear(RptTemplateValidation.class,
				Cnd.where("TEMPLATE_ID", "=", templateID).and("VALIDATION_TYPE", "=", validationType));
	}

	public void updateAValidationInfo(String validationId, String isRelationExplain, String relationExplainTextId) {
		if (isRelationExplain.equals("0")) {
			Sql strSql = Sqls.create("update RPT_TEMPLATE_VALIDATION set IS_RELATION_EXPLAIN = '" + isRelationExplain
					+ "',RELATION_EXPLAIN_ID = '' where VALIDATION_ID = '" + validationId + "'");
			dao.execute(strSql);
		} else if (isRelationExplain.equals("1")) {
			Sql strSql = Sqls.create("update RPT_TEMPLATE_VALIDATION set IS_RELATION_EXPLAIN = '" + isRelationExplain
					+ "',RELATION_EXPLAIN_ID = '" + relationExplainTextId + "' where VALIDATION_ID = '" + validationId
					+ "'");
			dao.execute(strSql);
		} else {

		}
	}

	public void deletaValidationById(String validationId) {
		dao.clear(RptTemplateValidation.class, Cnd.where("VALIDATION_ID", "=", validationId));
	}

	//获得一个校核公式根据ID
	public RptTemplateValidation getARptTemplateValidation(String validationId) {
		return dao.fetch(RptTemplateValidation.class, Cnd.where("VALIDATION_ID", "=", validationId));
	}

	//获得该报表模板所有表内校核
	public List<RptTemplateValidation> getAllTemplateInValidation(String templateID) {
		return dao.query(RptTemplateValidation.class, Cnd.where("TEMPLATE_ID", "=", templateID));
	}

	public List<RptTemplateValidation> getValidationByType(String templateId, ValidationType type) {
		String key = templateId + "_" + type;
		List<RptTemplateValidation> res = (List<RptTemplateValidation>) cache.get(CACHE_NAME, key);
		if (res == null) {
			res = dao.query(RptTemplateValidation.class,
					Cnd.where("TEMPLATE_ID", "=", templateId).and("VALIDATION_TYPE", "=", type.getKey()));
			if (res == null) {
				res = new ArrayList<>();
			}
			cache.put(CACHE_NAME, key, res);
		}
		return res;
	}

	/**
	 * 分页查询条件
	 * @param cnd
	 * @return
	 */
	public Map getValidationByTypeLigerUi(Condition cnd, int pageNum, int pageSize) {
		Map result = PageQuerys.ligerGridQuery(dao, RptTemplateValidation.class, cnd, pageNum, pageSize);
		return result;
	}

	public List<Exp> getValidationExpByType(String templateId, ValidationType type) {
		List<RptTemplateValidation> validations = getValidationByType(templateId, type);
		List<Exp> validationExps = new ArrayList<Exp>();
		for (RptTemplateValidation validation : validations) {
			Exp exp = new Exp(validation);
			validationExps.add(exp);
		}
		return validationExps;
	}

	public Map<String, List<Exp>> buildReportValidationMapByTemplate(String templateId) {
		Map<String, List<Exp>> validationMap = new HashMap<String, List<Exp>>();
		List<RptTemplateValidation> irow = getValidationByType(templateId, ValidationType.inner_row);
		validationMap.put(ValidationType.inner_row.getKey(), buildExpObj(irow));
		List<RptTemplateValidation> icol = getValidationByType(templateId, ValidationType.inner_col);
		validationMap.put(ValidationType.inner_col.getKey(), buildExpObj(icol));
		return validationMap;
	}

	private List<Exp> buildExpObj(List<RptTemplateValidation> validations) {
		List<Exp> list = new ArrayList<Exp>();
		for (RptTemplateValidation validation : validations) {
			list.add(new Exp(validation));
		}
		return list;
	}

	//传入报表ID和当前单元格位置，获得该单元格的ID
	public String getCellId(String templateId, String cellPosition, String areaType) {
		String numStr = cellPosition.replaceAll("[a-zA-Z]", "");
		String letterStr = cellPosition.replaceAll("[0-9]", "");
		String letterNum = CommonUtil.letterToNum(letterStr);
		Cell cell = null;
		if (areaType.equals("1")) {
			cell = dao.fetch(Cell.class, Cnd.where("TEMPLATE_ID", "=", templateId).and("REL_COL_NUM", "=", letterNum)
					.and("REL_ROW_NUM", "=", numStr));
		} else {
			cell = dao.fetch(Cell.class, Cnd.where("TEMPLATE_ID", "=", templateId).and("ABS_COL_NUM", "=", letterNum)
					.and("ABS_ROW_NUM", "=", numStr));
		}
		return cell.getCellId();
	}

	public RptBatchTemplateRel getARptBatchTemplateRel(String batchId, String templateId) {
		return dao.fetch(RptBatchTemplateRel.class,
				Cnd.where("BATCH_ID", "=", batchId).and("TEMPLATE_ID", "=", templateId));
	}
}
